package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "I Love My Work Work Work"
	mcFun(str)
}
func mcFun(str string) map[string]int {
	strs := strings.Fields(str)
	m := make(map[string]int)
	for _, word := range strs {
		s := word
		if _, has := m[word]; has {
			m[s]++
		} else {
			m[s] = 1
		}
	}
	fmt.Println(m)
	return m
}
func mapDamo01() {
	var m1 map[int]string  //只是声名一个map，没有初始化，为空（nil）map
	fmt.Println(m1 == nil) //true
	m2 := map[int]string{} //m2,m3的穿件方法是等价的
	m2[1] = "123"
	m3 := make(map[int]string)
	fmt.Println(m2, m3)
	m4 := make(map[int]string, 5) //声明时定义容量，len表示其容量
	fmt.Println(m4, len(m4))
}

//初始化map
func initMap() {
	var m1 map[int]string = map[int]string{1: "123", 5: "lisi", 3: "sundu", 2: "zhangsan"}
	m2 := map[int]string{1: "123", 5: "lisi", 3: "sundu", 2: "zhangsan"}
	fmt.Println(m1, m2)
}

//判断map中的key是否存在
func hasKey() {
	var m1 map[int]string = map[int]string{1: "123", 5: "lisi", 3: "sundu", 2: "zhangsan"}

	if v, has := m1[1]; has { //m1[下标]返回两个值，第一个是value，第二个是bool类型代表key是否存在
		fmt.Println("value=", v, "has=", has)
	} else {
		fmt.Println("false value=", v, "has=", has)
	}
}

//删除map
func deleteMap() {
	m := map[int]string{1: "123", 130: "456", 122: "789"}
	fmt.Println(m)
	delete(m, 130)
	fmt.Println(m)
}
